<?php

namespace App\Http\Model\Api;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use App\Http\Model\AudioApi;
use App\Http\Model\Base;
use Exception;

class AccompanyUser extends Base
{
    protected $table = 'accompany_user';
    protected $connection = "mysql_kkaudio";
    protected $primaryKey = 'id';
    protected $white = [];

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    public function beforeThrity($begin, $end) {
        $begin = strtotime($begin);
        $end = strtotime($end);
        $list = DB::connection($this->connection)
        ->table('accompany_order')
        ->leftJoin('user', 'user.user_id','=','accompany_order.accompany_user_id')
        ->select('accompany_order.accompany_user_id as user_id','user.nickname', 'user.portrait','user.uuid', DB::raw('SUM(number) as total_number'))
        ->where('accompany_order.status',3)
        ->whereBetween('accompany_order.created_at', [$begin, $end])
        ->groupBy('accompany_order.accompany_user_id')
        ->orderByRaw('total_number DESC')
        ->havingRaw('total_number > 0')
        ->limit(30)
        ->get();
        return self::objToArray($list);
    }

    public function userInfo(int $user_id, $begin, $end) {
        $begin = strtotime($begin);
        $end = strtotime($end);
        $list = DB::connection($this->connection)
        ->table('accompany_order')
        ->leftJoin('user', 'user.user_id','=','accompany_order.accompany_user_id')
        ->select('accompany_order.accompany_user_id as user_id','user.nickname', 'user.portrait','user.uuid', DB::raw('SUM(number) as total_number'))
        ->where('accompany_order.status',3)
        ->where('accompany_order.accompany_user_id', $user_id)
        ->whereBetween('accompany_order.created_at', [$begin, $end])
        ->groupBy('accompany_order.accompany_user_id')
        ->orderByRaw('total_number DESC')
        ->get()->all();
        if(!$list) {
            $res = DB::connection($this->connection)
            ->table('user')->where('user_id', $user_id)
            ->first(['user_id', 'nickname', 'portrait', 'uuid']);
            $res = self::objToArray($res);

            $hasPower = DB::connection($this->connection)
            ->table('accompany_user')
            ->whereRaw("user_id=$user_id AND status=1")->count();

            $list = [ array_merge($res, [ 'total_number' => $hasPower ? 0 : '未认证资质']) ];
        }
        return self::objToArray($list);
    }

    /**
     * 发放奖品
     */
    public function provideDiamond($begin, $end) {
        $begin = strtotime($begin);
        $end = strtotime($end);
        $lists = DB::connection($this->connection)
        ->table('accompany_order')
        ->leftJoin('user', 'user.user_id','=','accompany_order.accompany_user_id')
        ->select('accompany_order.accompany_user_id as user_id','user.nickname', 'user.portrait','user.uuid', DB::raw('SUM(number) as total_number'), DB::raw('SUM(pay_total_price) as total_price'))
        ->where('accompany_order.status',3)
        ->whereBetween('accompany_order.created_at', [$begin, $end])
        ->whereNotIn('accompany_order.accompany_user_id', $this->white)
        ->groupBy('accompany_order.accompany_user_id')
        ->orderByRaw('total_number DESC')
        ->havingRaw('total_number > 55')
        ->limit(30)
        ->get()->all();
        return $lists;
    }
}